import { defineStore } from "pinia";
import type { Category } from "@/api/product/model/categoryModel";
import {
  getCategory1List,
  getCategory2List,
  getCategory3List,
} from "@/api/product/category";

export const useCategoryStore = defineStore("categoryStore", {
  state: (): Category => {
    return {
      C1List: [],
      C2List: [],
      C3List: [],
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
    };
  },
  actions: {
    async getCategory1List() {
      this.C1List = await getCategory1List();
    },
    async getCategory2List() {
      if (!this.category1Id) return;
      this.C2List = await getCategory2List(this.category1Id as number);
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.C3List = [];
    },
    async getCategory3List() {
      if (!this.category2Id) return;
      this.C3List = await getCategory3List(this.category2Id as number);
      this.category3Id = undefined;
    },
  },
});
